﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Acsys.Entity;

namespace Acsys
{
    public partial class MainForm : Form, IMainForm
    {
        User m_currentUser;
        const string USER_FORM = "UserForm";
        const string CREDIT_TYPE_FORM = "CreditTypeForm";
        const string CURRENCY_FORM = "CurrencyForm";
        const string DEBIT_TYPE_FORM = "DebitTypeForm";
        const string DEBIT_TRANSACTION_FORM = "DebitTransactionForm";
        const string CREDIT_TRANSACTION_FORM = "CreditTransactionForm";
        const string DEBIT_TRANSACTION_REPORT_FORM = "DrTransactionReportForm";
        const string CREDIT_TRANSACTION_REPORT_FORM = "CrTransactionReportForm";
        const string VESSEL_FORM = "VesselForm";
        const string TOP_FORM = "TermOfPaymentForm";
        const string DEBIT_PAYMENT_TRANSACTION_REPORT_FORM = "DrPyTransactionReportForm";
        const string CREDIT_PAYMENT_TRANSACTION_REPORT_FORM = "CrPyTransactionReportForm";
        const string OWNER_FORM = "OwnerForm";

        public MainForm()
        {
            InitializeComponent();
            InitializeButtonClick();
            editToolStripMenuItem.Visible = false;
        }

        private void InitializeButtonClick()
        {
            treeViewMenu.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(treeView1_NodeMouseDoubleClick);
        }

        void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (e.Node.Name == "NodeUser")
            {
                if (isChild(USER_FORM)) { this.Cursor = Cursors.Default; return; }
                UserForm user = new UserForm(this, USER_FORM);
                user.WindowState = FormWindowState.Maximized;
                user.Show();
            }
            if (e.Node.Name == "NodeCreditType")
            {
                if (isChild(CREDIT_TYPE_FORM)) { this.Cursor = Cursors.Default; return; }
                CreditTypeForm user = new CreditTypeForm(this, CREDIT_TYPE_FORM);
                user.WindowState = FormWindowState.Maximized;
                user.Show();
            }
            if (e.Node.Name == "NodeCurrency")
            {
                if (isChild(CURRENCY_FORM)) { this.Cursor = Cursors.Default; return; }
                CurrencyForm user = new CurrencyForm(this, CURRENCY_FORM);
                user.WindowState = FormWindowState.Maximized;
                user.Show();
            }
            if (e.Node.Name == "NodeDebitType")
            {
                if (isChild(DEBIT_TYPE_FORM)) { this.Cursor = Cursors.Default; return; }
                DebitTypeForm user = new DebitTypeForm(this, DEBIT_TYPE_FORM);
                user.WindowState = FormWindowState.Maximized;
                user.Show();
            }
            if (e.Node.Name == "NodeDebitTransaction")
            {
                if (isChild(DEBIT_TRANSACTION_FORM)) { this.Cursor = Cursors.Default; return; }
                DebitTransactionForm user = new DebitTransactionForm(this, DEBIT_TRANSACTION_FORM);
                user.WindowState = FormWindowState.Maximized;
                user.Show();
            }
            if (e.Node.Name == "NodeCreditTransaction")
            {
                if (isChild(CREDIT_TRANSACTION_FORM)) { this.Cursor = Cursors.Default; return; }
                CreditTransactionForm user = new CreditTransactionForm(this, CREDIT_TRANSACTION_FORM);
                user.WindowState = FormWindowState.Maximized;
                user.Show();
            }
            if (e.Node.Name == "NodeDrTrRpt")
            {
                if (isChild(DEBIT_TRANSACTION_REPORT_FORM)) { this.Cursor = Cursors.Default; return; }
                DrTransactionReportForm user = new DrTransactionReportForm(this, DEBIT_TRANSACTION_REPORT_FORM);
                user.WindowState = FormWindowState.Maximized;
                user.Show();
            }
            if (e.Node.Name == "NodeCrTrRpt")
            {
                if (isChild(CREDIT_TRANSACTION_REPORT_FORM)) { this.Cursor = Cursors.Default; return; }
                CrTransactionReportForm user = new CrTransactionReportForm(this, CREDIT_TRANSACTION_REPORT_FORM);
                user.WindowState = FormWindowState.Maximized;
                user.Show();
            }
            if (e.Node.Name == "NodeOwner")
            {
                if (isChild(OWNER_FORM)) { this.Cursor = Cursors.Default; return; }
                OwnerForm user = new OwnerForm(this, OWNER_FORM);
                user.WindowState = FormWindowState.Maximized;
                user.Show();
            }
            if (e.Node.Name == "NodeVessel")
            {
                if (isChild(VESSEL_FORM)) { this.Cursor = Cursors.Default; return; }
                VesselForm user = new VesselForm(this, VESSEL_FORM);
                user.WindowState = FormWindowState.Maximized;
                user.Show();
            }
            if (e.Node.Name == "NodeTermOfPayment")
            {
                if (isChild(TOP_FORM)) { this.Cursor = Cursors.Default; return; }
                TermOfPaymentForm user = new TermOfPaymentForm(this, TOP_FORM);
                user.WindowState = FormWindowState.Maximized;
                user.Show();
            }
            if (e.Node.Name == "NodeDrPyRpt")
            {
                if (isChild(DEBIT_PAYMENT_TRANSACTION_REPORT_FORM)) { this.Cursor = Cursors.Default; return; }
                DrPyTransactionReportForm user = new DrPyTransactionReportForm(this, DEBIT_PAYMENT_TRANSACTION_REPORT_FORM);
                user.WindowState = FormWindowState.Maximized;
                user.Show();
            }
            if (e.Node.Name == "NodeCrPyRpt")
            {
                if (isChild(CREDIT_PAYMENT_TRANSACTION_REPORT_FORM)) { this.Cursor = Cursors.Default; return; }
                CrPyTransactionReportForm user = new CrPyTransactionReportForm(this, CREDIT_PAYMENT_TRANSACTION_REPORT_FORM);
                user.WindowState = FormWindowState.Maximized;
                user.Show();
            }
            this.Cursor = Cursors.Default;
        }
        bool isChild(string name)
        {
            bool result = false;
            foreach (Form ch in this.MdiChildren)
            {
                if (ch.Name == name)
                {
                    result = true;
                    ch.Activate();
                    break;
                }
            }
            return result;
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IChildForm frm = (IChildForm)this.ActiveMdiChild;
            if (frm == null) return;
            frm.Save(sender, e);
        }

        private void editToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            IChildForm frm = (IChildForm)this.ActiveMdiChild;
            if (frm == null) return;
            frm.Edit(sender, e);
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IChildForm frm = (IChildForm)this.ActiveMdiChild;
            if (frm == null) return;
            frm.Delete(sender, e);
        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IChildForm frm = (IChildForm)this.ActiveMdiChild;
            if (frm == null) return;
            frm.Clear(sender, e);
        }

        #region IMainForm Members

        public void EnableButtonSave(bool enable)
        {
            saveToolStripMenuItem.Enabled = enable;
        }

        public void EnableButtonEdit(bool enable)
        {
            editToolStripMenuItem1.Enabled = enable;
        }

        public void EnableButtonDelete(bool enable)
        {
            deleteToolStripMenuItem.Enabled = enable;
        }

        public void EnableButtonClear(bool enable)
        {
            clearToolStripMenuItem.Enabled = enable;
        }

        #endregion

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure to exit?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                this.Close();
        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (loginToolStripMenuItem.Text == "Log in")
            {
                LoginForm frm = new LoginForm(this,false);
                frm.ShowDialog();
                UserClarify();
            }
            else
            {
                foreach (Form c in this.MdiChildren)
                {
                    c.Close();
                }
                m_currentUser = null;
                toolStripStatusLabelUser.Text = "";
                panel1.Visible = false;
                editToolStripMenuItem.Visible = false;
                loginToolStripMenuItem.Text = "Log in";
                initTreeView();
                saveToolStripMenuItem.Enabled = false;
                editToolStripMenuItem1.Enabled = false;
                deleteToolStripMenuItem.Enabled = false;
                clearToolStripMenuItem.Enabled = false;
                refreshToolStripMenuItem.Enabled = false;
                lockToolStripMenuItem.Enabled = false;
                changePasswordToolStripMenuItem.Enabled = false;
                MainForm_Shown(null, null);
            }
        }

        private void initTreeView()
        {
            this.treeViewMenu.Nodes.Clear();
            System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("User", 1, 1);
            System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Credit Type", 2, 2);
            System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("Debit Type", 3, 3);
            System.Windows.Forms.TreeNode treeNode4 = new System.Windows.Forms.TreeNode("Currency", 4, 4);
            System.Windows.Forms.TreeNode treeNode5 = new System.Windows.Forms.TreeNode("Owner", 5, 5);
            System.Windows.Forms.TreeNode treeNode6 = new System.Windows.Forms.TreeNode("Vessel", 6, 6);
            System.Windows.Forms.TreeNode treeNode7 = new System.Windows.Forms.TreeNode("Term Of Payment", 7, 7);
            System.Windows.Forms.TreeNode treeNode8 = new System.Windows.Forms.TreeNode("Master", 0, 0, new System.Windows.Forms.TreeNode[] {
            treeNode1,
            treeNode2,
            treeNode3,
            treeNode4,
            treeNode5,
            treeNode6,
            treeNode7});
            System.Windows.Forms.TreeNode treeNode9 = new System.Windows.Forms.TreeNode("Debit Transaction", 9, 9);
            System.Windows.Forms.TreeNode treeNode10 = new System.Windows.Forms.TreeNode("Credit Transaction", 10, 10);
            System.Windows.Forms.TreeNode treeNode11 = new System.Windows.Forms.TreeNode("Transaction", 8, 8, new System.Windows.Forms.TreeNode[] {
            treeNode9,
            treeNode10});
            System.Windows.Forms.TreeNode treeNode12 = new System.Windows.Forms.TreeNode("Debit Transaction ", 9, 9);
            System.Windows.Forms.TreeNode treeNode13 = new System.Windows.Forms.TreeNode("Debit Payment report");
            System.Windows.Forms.TreeNode treeNode14 = new System.Windows.Forms.TreeNode("Credit Transaction ", 10, 10);
            System.Windows.Forms.TreeNode treeNode15 = new System.Windows.Forms.TreeNode("Credit Payment Report", 10, 10);
            System.Windows.Forms.TreeNode treeNode16 = new System.Windows.Forms.TreeNode("Report", 11, 11, new System.Windows.Forms.TreeNode[] {
            treeNode12,
            treeNode13,
            treeNode14,
            treeNode15});
            treeNode1.ImageIndex = 1;
            treeNode1.Name = "NodeUser";
            treeNode1.SelectedImageIndex = 1;
            treeNode1.Text = "User";
            treeNode2.ImageIndex = 2;
            treeNode2.Name = "NodeCreditType";
            treeNode2.SelectedImageIndex = 2;
            treeNode2.Text = "Credit Type";
            treeNode3.ImageIndex = 3;
            treeNode3.Name = "NodeDebitType";
            treeNode3.SelectedImageIndex = 3;
            treeNode3.Text = "Debit Type";
            treeNode4.ImageIndex = 4;
            treeNode4.Name = "NodeCurrency";
            treeNode4.SelectedImageIndex = 4;
            treeNode4.Text = "Currency";
            treeNode5.ImageIndex = 5;
            treeNode5.Name = "NodeOwner";
            treeNode5.SelectedImageIndex = 5;
            treeNode5.Text = "Owner";
            treeNode6.ImageIndex = 6;
            treeNode6.Name = "NodeVessel";
            treeNode6.SelectedImageIndex = 6;
            treeNode6.Text = "Vessel";
            treeNode7.ImageIndex = 7;
            treeNode7.Name = "NodeTermOfPayment";
            treeNode7.SelectedImageIndex = 7;
            treeNode7.Text = "Term Of Payment";
            treeNode8.ImageIndex = 0;
            treeNode8.Name = "NodeMaster";
            treeNode8.SelectedImageIndex = 0;
            treeNode8.Text = "Master";
            treeNode9.ImageIndex = 9;
            treeNode9.Name = "NodeDebitTransaction";
            treeNode9.SelectedImageIndex = 9;
            treeNode9.Text = "Debit Transaction";
            treeNode10.ImageIndex = 10;
            treeNode10.Name = "NodeCreditTransaction";
            treeNode10.SelectedImageIndex = 10;
            treeNode10.Text = "Credit Transaction";
            treeNode11.ImageIndex = 8;
            treeNode11.Name = "NodeTransaction";
            treeNode11.SelectedImageIndex = 8;
            treeNode11.Text = "Transaction";
            treeNode12.ImageIndex = 9;
            treeNode12.Name = "NodeDrTrRpt";
            treeNode12.SelectedImageIndex = 9;
            treeNode12.Text = "Debit Transaction ";
            treeNode13.ImageIndex = 9;
            treeNode13.Name = "NodeDrPyRpt";
            treeNode13.SelectedImageKey = "debittr.png";
            treeNode13.Text = "Debit Payment report";
            treeNode14.ImageIndex = 10;
            treeNode14.Name = "NodeCrTrRpt";
            treeNode14.SelectedImageIndex = 10;
            treeNode14.Text = "Credit Transaction ";
            treeNode15.ImageIndex = 10;
            treeNode15.Name = "NodeCrPyRpt";
            treeNode15.SelectedImageIndex = 10;
            treeNode15.Text = "Credit Payment Report";
            treeNode16.ImageIndex = 11;
            treeNode16.Name = "NodeReport";
            treeNode16.SelectedImageIndex = 11;
            treeNode16.Text = "Report";
            this.treeViewMenu.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
            treeNode8,
            treeNode11,
            treeNode16});
        }

        #region IMainForm Members


        public void SetUser(User user)
        {
            m_currentUser = user;
            toolStripStatusLabelUser.Text = user.NAME;
        }

        #endregion

        private void MainForm_Shown(object sender, EventArgs e)
        {
            LoginForm frm = new LoginForm(this,false);
            frm.ShowDialog();
            UserClarify();
        }

        private void UserClarify()
        {
            if (m_currentUser != null)
            {
                toolStripStatusLabelUser.Text = m_currentUser.NAME;
                panel1.Visible = true;
                editToolStripMenuItem.Visible = true;
                lockToolStripMenuItem.Enabled = true;
                changePasswordToolStripMenuItem.Enabled = true;
                loginToolStripMenuItem.Text = "Log out";
                if (!m_currentUser.FORM_USER_ACCESS) treeViewMenu.Nodes[0].Nodes["NodeUser"].Remove();
                if (!m_currentUser.FORM_TT_ACCESS) treeViewMenu.Nodes[0].Nodes["NodeCreditType"].Remove();
                if (!m_currentUser.FORM_DT_ACCESS) treeViewMenu.Nodes[0].Nodes["NodeDebitType"].Remove();
                if (!m_currentUser.FORM_CCY_ACCESS) treeViewMenu.Nodes[0].Nodes["NodeCurrency"].Remove();
                if (!m_currentUser.FORM_OWNER_ACCESS) treeViewMenu.Nodes[0].Nodes["NodeOwner"].Remove();
                if (!m_currentUser.FORM_VESSEL_ACCESS) treeViewMenu.Nodes[0].Nodes["NodeVessel"].Remove();
                if (!m_currentUser.FORM_TOP_ACCESS) treeViewMenu.Nodes[0].Nodes["NodeTermOfPayment"].Remove();

                if (!m_currentUser.FORM_DRTR_ACCESS) treeViewMenu.Nodes[1].Nodes["NodeDebitTransaction"].Remove();
                if (!m_currentUser.FORM_CRTR_ACCESS) treeViewMenu.Nodes[1].Nodes["NodeCreditTransaction"].Remove();

                if (!m_currentUser.FORM_DRTRRPT_ACCESS) treeViewMenu.Nodes[2].Nodes["NodeDrTrRpt"].Remove();
                if (!m_currentUser.FORM_CRTRRPT_ACCESS) treeViewMenu.Nodes[2].Nodes["NodeCrTrRpt"].Remove();

                if (!m_currentUser.FORM_DRTRRPT_ACCESS) treeViewMenu.Nodes[2].Nodes["NodeDrPyRpt"].Remove();
                if (!m_currentUser.FORM_CRTRRPT_ACCESS) treeViewMenu.Nodes[2].Nodes["NodeCrPyRpt"].Remove();

                if (treeViewMenu.Nodes["NodeMaster"].Nodes.Count == 0) treeViewMenu.Nodes["NodeMaster"].Remove();
                else treeViewMenu.Nodes["NodeMaster"].Expand();
                if (treeViewMenu.Nodes["NodeTransaction"].Nodes.Count == 0) treeViewMenu.Nodes["NodeTransaction"].Remove();
                else treeViewMenu.Nodes["NodeTransaction"].Expand();
                if (treeViewMenu.Nodes["NodeReport"].Nodes.Count == 0) treeViewMenu.Nodes["NodeReport"].Remove();
                else treeViewMenu.Nodes["NodeReport"].Expand();
            }
         }

        #region IMainForm Members


        public User CurrentUser
        {
            get { return m_currentUser; }
        }

        #endregion

        private void connectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SettingsForm frm = new SettingsForm(this);
            frm.ShowDialog();
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IChildForm frm = (IChildForm)this.ActiveMdiChild;
            if (frm == null) return;
            frm.Refresh(sender, e);
        }

        #region IMainForm Members


        public void EnableButtonRefresh(bool enable)
        {
            refreshToolStripMenuItem.Enabled = enable;
        }

        #endregion

        private void lockToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoginForm lf = new LoginForm(this, true);
            lf.StartPosition = FormStartPosition.CenterScreen;
            lf.ShowDialog();
        }

        private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChangePasswordForm frm = new ChangePasswordForm(m_currentUser);
            frm.ShowDialog();
        }
    }
}
